import { defineStore } from "pinia";
import { ref } from 'vue'
import userApi from '@/api/user.js'

// 用户模块
export const useUserStore = defineStore("user", () => {
    // 定义数据
    const token = ref('');
    //actions
    //设置新的token
    const setToken = function (newToken) {
        token.value = newToken;
    }
    // 移除token
    const removeToken = () => {
        token.value = '';
    }


    //登录用户的数据
    const user = ref({});
    //获取用户信息
    const getUser = async () => {
        //发送请求获取数据
        const result = await userApi.userGetInfo();
        console.log(result);
        user.value = result.data.data
    }
    //清空用户的信息
    const removeUser = async (obj) => {
        user.value = obj
    }
    return { token, setToken, removeToken, user, getUser ,removeUser }
},
    {
        persist: true
    }
);